package com.share.example.selenium.utils;

import com.alibaba.fastjson.JSON;
import com.share.common.utils.DelayUtil;
import com.share.common.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebElement;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ElementUtil {
    public static String inputValue(WebElement element, String context) {
        StringBuffer sb = new StringBuffer();
        int len = context.length();
        for (int i = 0; i < len; i++) {
            String c = "" + context.charAt(i);
            element.sendKeys(c);
            sb.append(c);
            DelayUtil.sleepRangMillis(300, 800);
        }
        return sb.toString();
    }

    public static String inputValue(WebElement element, String context, int millis_min, int millis_max) {
        StringBuffer sb = new StringBuffer();
        int len = context.length();
        for (int i = 0; i < len; i++) {
            String c = "" + context.charAt(i);
            element.sendKeys(c);
            sb.append(c);
            DelayUtil.sleepRangMillis(millis_min, millis_max);
        }
        return sb.toString();
    }

    //请求图片识别
    public static String getImageCode(String OCR_URL, WebElement divImage, int CODE_LEN, int TRY_MAX_COUNT, String model) {
        int tryCount = 0;
        while (tryCount < TRY_MAX_COUNT) {
            String result = null;
            Map<String, Object> param = new HashMap<>();
            try {
                if ("base64".equals(model)) {
                    String src = divImage.getAttribute("src");
                    String src_pre = "data:image/png;base64,";
                    String base64Images = src.substring(src_pre.length());
                    String url = OCR_URL + "/tesseract/base64";
                    param.put("image", base64Images);
                    result = HttpUtil.post(url, JSON.toJSONString(param), 10000);
                } else if ("url".equals(model)) {
                    String url = OCR_URL + "/tesseract/url";
                    String imageUrl = divImage.getAttribute("src");
                    param.put("resUrl", imageUrl);
                    result = HttpUtil.get(url, param, 10000);
                }

                //[[[[[3.0, 11.0], [127.0, 11.0], [127.0, 38.0], [3.0, 38.0]], ["NGSQU", 0.6469275951385498]]]]
                log.info("验证码识别,param={},请求结果res={}", JSON.toJSON(param), result);
                if (!StringUtils.isEmpty(result)) {
                    String[] split = result.split("\", 0.");
                    String code = split[0].split("\\[\"")[1];
                    if (code != null && code.length() == CODE_LEN) {
                        log.info("验证码值code={}", code);
                        return code;//直接退出
                    }
                }
            } catch (Exception e) {
                log.error("获取验证码异常,param={}", JSON.toJSON(param), e);
            }

            try {
                tryCount++;
                log.info("第{}次识别验证码码", tryCount);
                divImage.click();
                DelayUtil.sleepSecond(2);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
